package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Owner.MerchantOrOwner;
import com.sand.dto.merchant.ContentUpdate;
import com.sand.dto.merchant.ContractAdd;
import com.sand.dto.merchant.StageDelete;
import com.sand.entity.conclude.stage;
import com.sand.mapper.StageMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.StageService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StageServiceImpl extends ServiceImpl<StageMapper, stage> implements StageService {


    /**
     * 添加合同预购
     *
     * @param stage
     */
    @Override
    public Integer add(stage stage) {

        //参数校验
        pan(stage);

        save(stage);

        return 1;
    }

    /**
     * 修改合同预狗
     *
     * @param contentUpdate
     */
    @Override
    public Integer upd(ContentUpdate contentUpdate) {

        Integer contractid = contentUpdate.getContractid();

        Integer stageid = contentUpdate.getStageid();

        String meet = contentUpdate.getMeet();

        UpdateWrapper<stage> wrapper = new UpdateWrapper<>();
        //参数判断
        if (stageid == null && contractid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入要修改的id");
        }

        if (meet == null || meet.isEmpty()){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入约定时间");
        }

        if (stageid != null){
            wrapper.lambda().set(stage::getMeet,meet).eq(stage::getId,stageid);
        }

        if (contractid != null){
            wrapper.lambda().set(stage::getMeet,meet).eq(stage::getContractid,contractid);
        }

        if (stageid != null && contractid != null){
            wrapper.lambda().set(stage::getMeet,meet).eq(stage::getId,stageid).eq(stage::getContractid,contractid);
        }

        try {
            update(wrapper);
        } catch (Exception e) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"修改失败");
        }


        return 1;
    }

    /**
     * 查询所有预购
     */
    @Override
    public List<stage> all(MerchantOrOwner merchantOrOwner) {

        Integer ownerid = merchantOrOwner.getOwnerid();

        Integer merchantid = merchantOrOwner.getMerchantid();

        if (merchantid == null && ownerid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"为传入id");
        }

        if (merchantid != null && ownerid != null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不能同时查询多个id");
        }

        if (merchantid != null){
            return list(new LambdaQueryWrapper<stage>().eq(stage::getMerchantid,merchantid));
        }

        return list(new LambdaQueryWrapper<stage>().eq(stage::getOwnerid,ownerid));
    }

    /**
     * 删除预购
     *
     * @param stageDelete
     */
    @Override
    public Integer down(StageDelete stageDelete) {

        Integer contractid = stageDelete.getContractid();

        Integer stageid = stageDelete.getStageid();

        LambdaQueryWrapper<stage> wrapper = new LambdaQueryWrapper<>();

        if (stageid == null && contractid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入要删除的id");
        }

        if (stageid != null && contractid != null ){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不能同时删除多个id");
        }

        if (stageid != null){
            wrapper.eq(stage::getId,stageid);

            remove(wrapper);

            return 1;
        }

        wrapper.eq(stage::getContractid,contractid);

        try {
            remove(wrapper);
        } catch (Exception e) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"删除失败");
        }

        return 1;
    }

    /**
     * 添加合同的参数校验
     * @param stage
     */
    private void pan(stage stage) {

        //判断商家
        if (stage.getMerchantid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入甲方");
        }

        //判断客户
        if (stage.getOwnerid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入已方");
        }

        //判断时间
        if (stage.getMeet() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入约定的时间");
        }

        //判断合同
        if (stage.getContractid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入合同id");
        }

    }





}
